import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-03-02
 * Time: 15:33
 */
public class MyArrayList {//定义顺序表
    public int[] elem;
    public int UsedSize;

    public MyArrayList(){
        this.elem=new int[10];
        //构造方法
    }

     // 打印顺序表
     public void display() {
         for (int i = 0; i < UsedSize; i++) {
             System.out.print(this.elem[i]+" ");
         }
         System.out.println();
     }
    // 获取顺序表有效数据长度
    public int size() {
        return this.UsedSize;
    }
     // 在 pos 位置新增元素
     public void add(int pos, int data) {
        if (pos<0||pos>this.UsedSize){
            System.out.println("pos位置不合法!");
        }
        if (isFull()){
            this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
        }
         for (int i = UsedSize-1; i >pos ; i--) {
             this.elem[i+1]=this.elem[i];
         }
         this.elem[pos]=data;
         UsedSize++;
     }
     // 判断数组满不满
    public boolean isFull(){
        return this.UsedSize==this.elem.length;
    }
     // 判定是否包含某个元素
     public boolean contains(int toFind) {
         for (int i = 0; i < this.elem.length; i++) {
             if (elem[i]==toFind){
                 return true;
             }
         }
        return false;
    }
     // 查找某个元素对应的位置
     public int search(int toFind) {
         for (int i = 0; i < this.elem.length; i++) {
             if (toFind == this.elem[i]) {
                 return i;
             }
         }
        return -1;
    }
     // 获取 pos 位置的元素
     public int getPos(int pos) {
         for (int i = 0; i < this.elem.length; i++) {
             if (pos==this.elem[i]){
                 return i;
             }
         }
        return -1;
    }
     // 给 pos 位置的元素设为 value
     public void setPos(int pos, int value) {
        if (pos<0||pos>this.UsedSize){
            System.out.println("pos位置不合法！");
        }
         for (int i = 0; i < this.elem.length; i++) {
             if(i==pos){
                 this.elem[pos]=value;
             }
         }
     }
     //删除第一次出现的关键字key
     public void remove(int toRemove) {
         for (int i = 0; i < this.UsedSize; i++) {
             if (this.elem[i]==toRemove){
                 for (int j = i; j <this.UsedSize-1 ; j++) {
                     this.elem[j]=this.elem[j+1];
                 }
             }
         }
     }

     // 清空顺序表
     public void clear() {
        this.UsedSize=0;
     }

}
